
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Omkar
 */
public class XmlParser
{
    private String filepath = null;
    private Document xmlDocument = null;
    private String jobID = null;
    private String command = null;
    private String flagName = null;
    private String stdOutAttribute = null;
    private String stdErrAttribute = null;
    private ArrayList<Argument> arguments = null;

    public String getJobID()
    {
        return jobID;
    }

    public String getCommand()
    {
        return command;
    }

    public String getFlagName()
    {
        return flagName;
    }

    public String getStdOutAttribute()
    {
        return stdOutAttribute;
    }

    public String getStdErrAttribute()
    {
        return stdErrAttribute;
    }

    public ArrayList<Argument> getArguments()
    {
        return arguments;
    }
    
    private Document CreateInstanceofDocument(String filePath)
    {
        Document document = null;

        try
        {
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            File file = new File(filePath);
            document = documentBuilder.parse(file);
        }
        catch(Exception exception)
        {
            System.out.println(exception);
        }

        return document;
    }

    public XmlParser(String filePath)
    {
        filePath = "C:\\Users\\Omkar\\Desktop\\test.xml";
        jobID = new String();
        command = new String();
        flagName = new String();
        stdOutAttribute = new String();
        stdErrAttribute = new String();
        arguments = new ArrayList<Argument>();
        xmlDocument = CreateInstanceofDocument(filePath);
        ExtractXmlValues();
    }

    private void ExtractXmlValues()
    {
        Element element = xmlDocument.getDocumentElement();
        element.normalize();
        jobID = element.getAttribute("job");
        NodeList nodeList = element.getChildNodes();
        
        for (int i = 0 ; i < nodeList.getLength(); i++)
        {
            Node node = nodeList.item(i);

            if(node.getNodeType() == Node.ELEMENT_NODE)
            {                                
                Element childElement = (Element)nodeList.item(i);
                NodeList nodes = childElement.getChildNodes();
                
                for (int j = 0; j < nodes.getLength(); j++)
                {
                    Node childNode = nodes.item(j);
                    
                    if(childNode.getNodeType() == Node.ELEMENT_NODE)
                    {
                        //Element ele = (Element)childNode;
                        String name = childNode.getNodeName();
                        //Node temp = null;

                        if(name.equals("command"))
                        {
                            command = getCommandValue(childNode);
                        }
                        else if (name.equals("arg"))
                        {
                            arguments.add(getArgumentValue(childNode));
                        }
                        else if (name.equals("flag"))
                        {
                            flagName = getFlagValue(childNode);
                        }
                        else if (name.equals("stdout"))
                        {
                            stdOutAttribute = getToAttribute(childNode);
                        }
                        else if (childNode.getNodeName().equals("stderr"))
                        {
                            stdErrAttribute = getToAttribute(childNode);
                        }                        
                    }
                }                
            }
        }                
    }
    
    private String getToAttribute(Node node)
    {
        return getAttribute(node, "to");
    }

    private String getFlagValue(Node childNode)
    {
        return getNodeValue(childNode, "name");
    }

    private Argument getArgumentValue(Node childNode)
    {
        String name = getNodeValue(childNode, "name");
        String value = getNodeValue(childNode, "value");
        String valueAttribute = getAttribute(childNode, "value", "usage");

        Argument argument = new Argument(name, value, valueAttribute);

        return argument;
    }

    private String getCommandValue(Node childNode)
    {
        return childNode.getFirstChild().getNodeValue();
    }

    private String getNodeValue(Node node, String tagName)
    {
        String value = "";
        Element ele = (Element) node;
        NodeList nl = ele.getElementsByTagName(tagName);

        if(nl != null && nl.getLength() > 0)
        {
            Element el = (Element)nl.item(0);
            value = el.getFirstChild().getNodeValue();
        }

        return value;
    }

    private String getAttribute(Node node, String tagName, String attributeName)
    {
        String value = "";
        Element ele = (Element) node;
        NodeList nl = ele.getElementsByTagName(tagName);

        if(nl != null && nl.getLength() > 0)
        {
            Element el = (Element)nl.item(0);
            value = el.getAttribute(attributeName);
        }

        return value;
    }

    private String getAttribute(Node node, String attributeName)
    {
        Element ele = (Element) node;
        return ele.getAttribute(attributeName);
    }   

    public String getExecutableCommand()
    {
        StringBuilder executableCommand = new StringBuilder();

        executableCommand.append(command);
        executableCommand.append(getArgumentValues());
        executableCommand.append(" -");
        executableCommand.append(getFlagName());
        executableCommand.append(" 1> ");
        executableCommand.append(getStdOutAttribute());
        executableCommand.append(" 2> ");
        executableCommand.append(getStdErrAttribute());
        executableCommand.append(" &");
        return executableCommand.toString();
    }

    private String getArgumentValues()
    {
        StringBuilder argumentValues = new StringBuilder();

        for (Iterator<Argument> counter = getArguments().iterator(); counter.hasNext();)
         {
             Argument argument = counter.next();
             argumentValues.append(" -");
             argumentValues.append(argument.getName());
             argumentValues.append(" /");
             argumentValues.append("jobs/");
             argumentValues.append(jobID);             

             if(!argument.getValueAttribute().isEmpty())
             {
                argumentValues.append("/");
                argumentValues.append(argument.getValueAttribute());
                argumentValues.append("/");
             }
             
             argumentValues.append(argument.getValue());             
         }
        
        return argumentValues.toString();
    }

    public class Argument
    {
        private String name = null;
        private String value = null;
        private String valueAttribute = null;

        public Argument(String name, String value, String valueAttribute)
        {
            this.name = name;
            this.value = value;
            this.valueAttribute = valueAttribute;
        }

        public String getName()
        {
            return name;
        }

        public String getValue ()
        {
            return value;
        }

        public String getValueAttribute()
        {
            return valueAttribute;
        }
    }
}